package com.bobi.tfca.ui.member

import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.bobi.tfca.network.ApiResponse
import com.bobi.tfca.network.api.MessageAPI
import com.bobi.tfca.network.response.MemberResponse
import com.bobi.tfca.ui.adapter.GroupMemRmAdapter
import com.bobi.tfca.util.LogUtil
import com.bobi.tfca.viewmodel.PublicViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class MemberRmFragmentVM : ViewModel() {

    val members by lazy {
        MutableLiveData(ArrayList<MemberResponse>())
    }

    /**
     * 移除群员
     */
    fun removeGroupMember(
        publicViewModel: PublicViewModel?,
        fragment: MemberRemoveFragment,
        memberIds: ArrayList<Int>,
        adapter: GroupMemRmAdapter
    ) {
        val groupId = publicViewModel!!.chatRoom.value!!.groupId
        publicViewModel.apply {
            request(MessageAPI::class.java).removeMembers(memberIds, groupId).getResponse {
                it.collect {
                    when (it) {
                        is ApiResponse.Error -> {
                            LogUtil.e(it.errMsg)
                        }

                        ApiResponse.Loading -> LogUtil.e("Loading")
                        is ApiResponse.Success -> {
                            withContext(Dispatchers.Main) {
                                val code = it.data?.code
                                if (code == 200) {
                                    // 重置已经选择的会员
                                    adapter.setCheckedMemberList(arrayListOf())
                                    findAllGroupMembers(publicViewModel, groupId, fragment)
                                    Toast.makeText(fragment.requireActivity(), "移除成功", Toast.LENGTH_SHORT).show()
                                } else {
                                    Toast.makeText(fragment.requireActivity(), "移除失败", Toast.LENGTH_SHORT).show()
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 查询群成员
     */
    fun findAllGroupMembers(
        publicViewModel: PublicViewModel,
        groupId: Int,
        fragment: MemberRemoveFragment
    ) {
        publicViewModel.apply {
            request(MessageAPI::class.java).findAllGroupMembers(groupId).getResponse {
                it.collect {
                    when (it) {
                        is ApiResponse.Error -> {
                            LogUtil.e(it.errMsg)
                        }

                        ApiResponse.Loading -> LogUtil.e("Loading")
                        is ApiResponse.Success -> {
                            withContext(Dispatchers.Main) {
                                // 赋值
                                members.value = it.data!!.data as ArrayList<MemberResponse>
                            }
                        }
                    }
                }
            }
        }
    }

}